package com.example.leetcode;

import java.util.*;

public class HW09 {
    public static void main(String[] args) {
        String s="mMbccbc";
        char[] chars = s.toCharArray();
        HashMap<Integer,String> map = new HashMap<>();
        int i=0;
        for (char c:chars){
           String tagert =isMax(map);
            if (tagert.equals(String.valueOf(c))){
                int key = getKey(map,String.valueOf(c));
                map.remove(key);
            }else {
                map.put(i,String.valueOf(c));
            }
            i++;
        }


        System.out.println(map.size());
    }

    //通过遍历，根据value 获取key
    private static int getKey(HashMap<Integer, String> map, String valueOf) {
        List<Map.Entry<Integer,String>> maps= new ArrayList<>(map.entrySet());
        for (Map.Entry<Integer,String> m:maps){
            if (m.getValue().equals(valueOf)){
                return m.getKey();
            }
        }
        return -1;
    }

    private static String isMax(HashMap<Integer,String> map) {
        if (map.isEmpty()){
            return "";
        }
        //获取map key 集合
      Set<Integer> list = map.keySet();
        System.out.println(map.values());
        Collection values = map.values();
      Integer num =list.stream().reduce(Integer::max).get();
     return map.get(num);
    }
}
